﻿Public Class clsSparkEmitter
    Private _sparkList As List(Of clsSpark)

    Private _sparkSpread As Single

    Public Sub New(ByVal sparkSpread As Single)
        _sparkList = New List(Of clsSpark)
        _sparkSpread = sparkSpread
    End Sub

    Public Property SparkSpread As Single
        Get
            Return _sparkSpread
        End Get
        Set(ByVal value As Single)
            _sparkSpread = value
        End Set
    End Property
    
    Public Sub Emit(ByVal pos As Drawing.PointF, ByVal sparks As Integer)
        Emit(pos, sparks, Drawing.Color.Yellow)
    End Sub
    Public Sub emit(ByVal pos As Drawing.PointF, ByVal sparks As Integer, ByVal rainbow As Boolean)
        If rainbow Then
            _Emit(pos, sparks)
        Else
            _Emit(pos, sparks, Drawing.Color.Yellow)
        End If
    End Sub
    Public Sub Emit(ByVal pos As Drawing.PointF, ByVal sparks As Integer, ByVal color As Drawing.Color)
        _Emit(pos, sparks, color)
    End Sub
    Private Sub _Emit(ByVal pos As Drawing.PointF, ByVal sparks As Integer, Optional ByVal color As Drawing.Color = Nothing)
        Dim rainbow As Boolean = False
        If color = Drawing.Color.Empty Then
            rainbow = True
        End If
        Dim rndVel As Drawing.Point
        Dim newSpark As clsSpark
        For i = 0 To sparks - 1
            If rainbow Then
                color = clsSim.RndColor()
            End If
            rndVel = New Drawing.Point(clsSim.RndRange(-_sparkSpread, _sparkSpread), clsSim.RndRange(-_sparkSpread, _sparkSpread))
            newSpark = New clsSpark(pos, rndVel, color, clsSim.RndRange(5, 15) / 10)
            _sparkList.Add(newSpark)
        Next
    End Sub


    Public Sub Draw(ByRef g As Graphics)
        For i = _sparkList.Count - 1 To 0 Step -1
            _sparkList(i).Draw(g)
            If _sparkList(i).Faded Then
                _sparkList.RemoveAt(i)
            End If
        Next
    End Sub
End Class
